<?php
define('DEFAULT_BR_TEXT', "\r\n");
set_time_limit(999999999999);
class get_data_html {

	function get_content($url,$aut='')
	{
		$url = str_replace(" ","%20",$url);
		if($aut != '')
			$url =str_replace("http://","http://".$aut,$url);
		$t = file_get_contents($url);
		return $t;
	}
	function get_data($url, $ref_url='')
	{
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
		if($ref_url !='')
		curl_setopt($ch, CURLOPT_REFERER, $ref_url);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
	function get_data_post($url, $form_data ,$ref_url='',$arr_header= array())
	{
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		if(count($arr_header)>0)
		curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
		if($ref_url !='')
		curl_setopt($ch, CURLOPT_REFERER, $ref_url);
		
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
		
		$data = curl_exec($ch);
		curl_close($ch);

		return $data;
	}
	
	function load($selector_string,$array)
	{
		$start =$array['estart'];
		$end =$array['eend'];
		$pattern = "/".$start."(.*?)".$end."/is";
		preg_match_all($pattern, trim((string)$selector_string).'', $matches);
		return $matches[1];
	}
	   // find elements by css selector
	function find( $begin='', $end='',$arrcontent) {
		$arr = array();
		for($i=0; $i<count($arrcontent);$i++){
			$arr[] = $this->GetBetween($arrcontent[$i],$begin,$end);
		}
        return $arr;
	}
	function find_html_all($tagname, $b,$content,$fix='"')
	{
		$arr = array();
		for($i=0; $i<count($content);$i++){
			$arr[] = $this->find_html($tagname,$b,$content[$i],$fix);
		}
        return $arr;
	}
	function find_html($tag, $b,$content, $fix='"')
	{
		if ($b==''){
			$regexp = "<$tag(.*)>(.*)<\/$tag>";
			if(preg_match("/$regexp/siU", $content, $matches)) {
				return $matches;
				// $matches[2] = array of link addresses
				// $matches[3] = array of link text - including HTML code
			}
		}
		else if(in_array($tag, array('a','b','p','div','strong'))){
			$regexp = "<$tag\s[^>]*$b=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/$tag>";
			if(preg_match("/$regexp/siU", $content, $matches)) {
				return $matches;
				// $matches[2] = array of link addresses
				// $matches[3] = array of link text - including HTML code
			}
		}
		else if(in_array($tag, array('img')))
		{
			$regexp = "<$tag\s[^']*?$b=$fix(.*?)$fix.*?>";
			if(preg_match("/$regexp/i", $content, $matches)) {
				return $matches;
			}
		}
		return '';
	}
	function GetBetween($content,$start,$end){
		$r = explode($start, $content);
		if (isset($r[1])){
			$r = explode($end, $r[1]);
			return $r[0];
		}
		return '';
	}	
	function removetag($start, $end, $content)
	{
		$string=preg_replace("/$start(.+?)$end/is", "",$content);
		//$string = preg_replace("/$start\s+[^>]*$end/i", "", $content); 
		return $string;
	}
	function convert_html_char($content)
	{
		$content = str_replace(array("'"),array("\'"),$content);
		$content = str_replace(array('"'),array('\"'),$content);
		return $content;
	}
	function get_data_login($POSTFIELDS,$LOGINURL,$GETURL)
	{
		//$LOGINURL = "http://" . $_GET['l'] . "/user/login/";
		//$GETURL = "http://" . $_GET['l'] . "/present/download/entry_id/" . $_GET['id'];
		//$POSTFIELDS = "ajax=0&message=&username=$id&password=$pw&remember=1";
		
		$cookieFile = $this->AUTH_SITE_COOKIE_STORE($LOGINURL,$POSTFIELDS);
 
		$result = $this->AUTH_SITE_GET($GETURL,$cookieFile);
		ob_flush();
		flush();
		return $result;
		
	}
	function AUTH_SITE_COOKIE_STORE($LOGINURL,$POSTFIELDS, $file_name = 'cookie_deal.txt')
	{
		$parseURL = parse_url($LOGINURL);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_COOKIEJAR, $file_name);
		curl_setopt($ch, CURLOPT_URL,"$LOGINURL");
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,   $POSTFIELDS  );
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		 //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		// curl_setopt($ch, CURLOPT_BINARYTRANSFER, 0);
		//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));  
		curl_setopt($ch,CURLOPT_HTTPHEADER, array(
    'Accept: */*',
    'Connection: Keep-Alive',
    // this allows you transfer binary data through POST
    'Content-type: multipart/form-data'
    ));  
		//curl_setopt($ch, CURLOPT_REFERER, 'http://mp3.zing.vn/');
		$html = curl_exec ($ch);
			$filenum=@fopen($file_name,"w");
		flock($filenum,LOCK_EX);
		$file_data=fwrite($filenum,$html);
		fclose($filenum);
	
		
		curl_close ($ch);
		return $file_name;
		ob_flush();
		flush();
	}
	function AUTH_SITE_GET($GETURL,$cookieFile)
	{
		$parseURL = parse_url($GETURL);
		$ca = curl_init();
		curl_setopt($ca, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ca, CURLOPT_COOKIEFILE, "$cookieFile");
		curl_setopt($ca, CURLOPT_URL,"$GETURL");
		$result = curl_exec ($ca);
		ob_flush();
		flush();
		curl_close ($ca);
		return $result;
	}
	 function googlebot_get( $url ) 
    { 
        $curl=curl_init();
		curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (http://www.googlebot.com/bot.html)');
		curl_setopt($curl, CURLOPT_URL,$url);
		curl_setopt($curl, CURLOPT_AUTOREFERER, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,2);
		$buffer = curl_exec($curl);
		$html = @mb_convert_encoding($buffer, 'HTML-ENTITIES', 'utf-8');
		curl_close($curl);
		if (empty($html)){
			return "Nothing to display";
		}
		else{
			return $html;
		}
    }
}
?>